#include "AcronymDlg.h"
#include "resource.h"

AVWindow AcronymDlg::gAVWindow = NULL;
ADMListRef AcronymDlg::_list;


AcronymDlg::AcronymDlg(void):
	_pText(NULL),
	_pCurrentValue(NULL),
	_pValues(NULL)
{
}

AcronymDlg::~AcronymDlg(void)
{
}



void AcronymDlg::DoModal(AcronymDlg *pAcronymDlg)
{
	//Make sure resource chain is correct
	ADMUtils::InitializeADM();

	// Display modal dialog to get user input
	sADMDialog->Modal(sADMPluginRef,
					"TASC:Extrakt",
					IDD_ACRONYM,
					kADMModalDialogStyle,
					AcronymDlg::SettingsDialogOnInit, 
					(ADMUserData) pAcronymDlg,
					NULL);

	ADMUtils::ReleaseADM();
}

static CString GetControlText(ADMDialogRef dialogRef, ASInt32 itemId)
{
	ADMItemRef textEdit = sADMDialog->GetItem (dialogRef, itemId);
	ASInt32 textLen = sADMItem->GetTextLengthW (textEdit);	

	ADMUnicode msg[512];
	sADMItem->GetTextW (textEdit, msg, 512);
	//pass in size in bytes for text
	return CString((wchar_t*)msg);
}

void ASAPI AcronymDlg::OnAddButton (ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialog = sADMItem->GetDialog (itemRef);
	AcronymDlg* acronymDlg = (AcronymDlg* ) sADMDialog->GetUserData(dialog);

	acronymDlg->_pValues->push_back(GetControlText(dialog, IDC_ALT_NEW));

	int i = acronymDlg->_pValues->size() - 1;

	*(acronymDlg->_newValueIndex) = i;
	
	ADMEntryRef entry = sADMList->InsertEntry(_list, i+1);

	CString &value = (*(acronymDlg->_pValues))[i];
	sADMEntry->SetTextW(entry, (ASUnicode*)value.GetBuffer());
	sADMEntry->Select(entry, true);

	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);

	ADMItemRef setItem = sADMDialog->GetItem(dialog, IDSET);
	sADMItem->Enable(setItem, true);
}

void ASAPI AcronymDlg::OnSetButton (ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialog = sADMItem->GetDialog (itemRef);
	AcronymDlg* acronymDlg = (AcronymDlg* ) sADMDialog->GetUserData(dialog);
	acronymDlg->_action = AcronymDlg::Process;
	
	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);
}

void ASAPI AcronymDlg::OnSkipButton (ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialog = sADMItem->GetDialog (itemRef);
	AcronymDlg* acronymDlg = (AcronymDlg* ) sADMDialog->GetUserData(dialog);
	acronymDlg->_action = AcronymDlg::Skip;

	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);
	
	sADMDialog->EndModal(dialog, IDSKIP, 0);
}

void ASAPI AcronymDlg::OnStopButton (ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	ADMDialogRef dialog = sADMItem->GetDialog (itemRef);
	AcronymDlg* acronymDlg = (AcronymDlg* ) sADMDialog->GetUserData(dialog);
	
	acronymDlg->_action = AcronymDlg::Stop;

	// Finally, we exhibit the default behavior.
	sADMItem->DefaultNotify (itemRef, notifierRef);
	
	sADMDialog->EndModal(dialog, IDSTOP, 0);
}

void ASAPI AcronymDlg::OnListClick (ADMItemRef itemRef, ADMNotifierRef notifierRef)
{
	if(notifierRef)
	{
		if (! sADMNotifier->IsNotifierType( notifierRef, kADMUserChangedNotifier ) )
			return;
		//UpdateInfo(gCurrentItemNum);
		sADMItem->DefaultNotify(itemRef, notifierRef);
	}

	ADMDialogRef dialog = sADMItem->GetDialog (itemRef);
	AcronymDlg* acronymDlg = (AcronymDlg* ) sADMDialog->GetUserData(dialog);

	ADMEntryRef entry = sADMList->GetActiveEntry(_list);
	*(acronymDlg->_newValueIndex) = sADMEntry->GetIndex(entry);

	ADMItemRef setItem = sADMDialog->GetItem(dialog, IDSET);
	sADMItem->Enable(setItem, true);
}

ASErr ASAPI AcronymDlg::SettingsDialogOnInit(ADMDialogRef dialogRef)
{
	ADMItemRef itemRef;

	// Insert an override for the Set button.
	itemRef = sADMDialog->GetItem (dialogRef, IDADD);
	sADMItem->SetNotifyProc (itemRef, AcronymDlg::OnAddButton);

	itemRef = sADMDialog->GetItem (dialogRef, IDSET);
	sADMItem->Enable(itemRef, false);
	sADMItem->SetNotifyProc (itemRef, AcronymDlg::OnSetButton);
	
	itemRef = sADMDialog->GetItem (dialogRef, IDSKIP);
	sADMItem->SetNotifyProc (itemRef, AcronymDlg::OnSkipButton);

	itemRef = sADMDialog->GetItem (dialogRef, IDSTOP);
	sADMItem->SetNotifyProc (itemRef, AcronymDlg::OnStopButton);

	itemRef = sADMDialog->GetItem (dialogRef, IDC_LIST);
	sADMItem->SetNotifyProc (itemRef, AcronymDlg::OnListClick);


	// Tweaking required to behave correctly within Acrobat.
	sADMDialog->SetDestroyProc (dialogRef, AcronymDlg::OnDestroy);
	gAVWindow = AVWindowNewFromPlatformThing (AVWLmodal, AVWIN_WANTSKEY, NULL,
									gExtensionID, sADMDialog->GetWindowRef(dialogRef));
	AVAppBeginModal (gAVWindow);

	// set items' values
	return SetDialogDatabase(dialogRef);
}

ASErr ASAPI AcronymDlg::SetDialogDatabase(ADMDialogRef dialog)
{
	// get dialog's user data
 	AcronymDlg* acronymDlg = (AcronymDlg* ) sADMDialog->GetUserData(dialog);


	// set items' values

	ADMItemRef item = sADMDialog->GetItem(dialog, IDC_LIST);
	_list = sADMItem->GetList(item);

	for(unsigned int i=0; i < acronymDlg->_pValues->size(); ++i)
	{
		ADMEntryRef entry = sADMList->InsertEntry(_list, i+1);

		CString &value = (*(acronymDlg->_pValues))[i];
		sADMEntry->SetTextW(entry, (ASUnicode*)value.GetBuffer());
		
		if(acronymDlg->_pCurrentValue && value == *(acronymDlg->_pCurrentValue))
		{
			sADMEntry->Select(entry, true);
			*(acronymDlg->_newValueIndex) = i;
		}
	}
	
	if(acronymDlg->_pText)
	{
		item = sADMDialog->GetItem(dialog, IDC_TEXT);
		sADMItem->SetTextW (item, (ASUnicode*)acronymDlg->_pText->GetBuffer());
	}

	if(acronymDlg->_pCurrentValue)
	{
		item = sADMDialog->GetItem(dialog, IDC_ALT);
		sADMItem->SetTextW (item, (ASUnicode*)acronymDlg->_pCurrentValue->GetBuffer());
	}
	
	return kSPNoError;
}

void ASAPI AcronymDlg::OnDestroy (ADMDialogRef dialogRef)
{
	AVAppEndModal();
	if (NULL != gAVWindow)
		AVWindowDestroy (gAVWindow);
}